package com.daxue.nacosprovider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

    @RestController
    class EchoController {

        @Autowired
        KafkaProducer kafkaProducer;

        @GetMapping("/echo/{string}")
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }

        /**
         * 发送一个消息到kafka中，测试通过，但是发送的是HashMap的时候，报错不能转化为String，不知道为什么。
         * @param value
         * @return
         */
        @GetMapping("/send/{value}")
        public String test(@PathVariable String value) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("key", value);
            kafkaProducer.send(map);
            return "1";
        }
    }

    @Value("${user}")
    public String user;

    @Bean
    public Object test() {
        System.out.println("user:" + user);
        return new Object();
    }

}
